// pages/Achievement/Achievement.js
const app = getApp();
var wxCharts = require('../../utils/js/charts.js');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        "mydate": {
            "nowdate": "",//查询的时间
            "datestr": ""//显示在界面上的时间字符串
        },
        "bossach": {},
        "shoplist":[],//店铺列表
        "selectshop":{},//当前选中的店铺
        "isshow":false,
        "unitarry": ["", "天", "周", "月"],
        "datefield":["","day","day","month"],
        "dateType": 2,
        "colorarry": ["#CC3333", "#FF9999", "#99CC33", "#FF9966", "#00CC00", "#99CCFF", "#336699", "#339933", "#339999", "#663366"]
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this = this;
        var dateType = options.dateType ? parseInt(options.dateType):1 ;
        var myDate = new Date();
        var date = myDate, datestr = myDate;
        switch (dateType){
            case 1://天
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                datestr=date;
            break;
            case 2://周
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                //获取第几周
                var weeknum = app.common.getMonthWeek(date);
                datestr = date + "(第" + weeknum + "周)";
            break;
            case 3://月
                date = app.common.dateFormat(myDate, 'yyyy-MM');
                datestr = date;
                date=date+'-01';
            break;
        }
        _this.setData({
            mydate: {
                nowdate: date,
                datestr: datestr
            },
            dateType: dateType
        })
        _this.GetShopList(function(){
            _this.GetDetail();
        });
    },
    //加/减
    add: function (e) {
        var dataset = e.currentTarget.dataset;
        var size = parseInt(dataset.amount);
        //单日
        let date = this.data.mydate.nowdate;
        var datestr = date;
        var dateType = parseInt(this.data.dateType);
        switch (dateType) {
            case 1://天
                date = app.common.dateAdd("dd", size, date, "yyyy-MM-dd");
                datestr = date;
                break;
            case 2://周
                date = app.common.dateAdd("week", size, date, "yyyy-MM-dd");
                //获取第几周
                var weeknum = app.common.getMonthWeek(date);
                datestr = date + "(第" + weeknum + "周)";
                break;
            case 3:
                date = app.common.dateAdd("mm", size, date, "yyyy-MM");
                datestr = date;
                date=date+'-01';
        }
        this.setData({
            mydate: {
                nowdate: date,
                datestr: datestr
            }
        })
        this.GetDetail();
    },
    //获取详情数据
    GetDetail: function () {
        var _this = this;
        wx.showLoading({
            title: '加载中',
        });
        var reg = _this.data.selectshop.RegisterNumber,
            dateType = _this.data.dateType,
            date =_this.data.mydate.nowdate;
        app.http.POST('/MiniManagerAchievement/GetBossAchievementDetail', {
            "reg": reg,
            "dateType": dateType,
            "date": date
        }, (err, data) => {
            if (data.code == 1) {
                _this.setData({
                    "bossach": data.data
                });
                //生成报表数据
                _this.SetReportChart();
                wx.hideLoading();
            } else if (data.code == 2) {
                wx.hideLoading();
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
            else {
                wx.hideLoading();
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    },
    //获取店铺列表
    GetShopList:function(fn){
        var _this=this;
        app.http.POST('/MiniManagerShop/GetShopList',{},(err,data)=>{
            if(data.code==1){
                if (data.data.length<1){
                    return;
                }
                _this.setData({
                    shoplist:data.data,
                    selectshop: data.data[0]
                });
                fn && fn();
            } else if (data.code == 2) {
                wx.hideLoading();
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
            else {
                wx.hideLoading();
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    },
    ShowShop:function(){
        var _this=this;
        var isshow = _this.data.isshow;
        isshow=isshow?false:true;
        _this.setData({
            isshow:isshow
        });
    },
    SelectShop:function(e){
        var dataset = e.currentTarget.dataset;
        this.setData({
            isshow:false,
            selectshop:{
                ID: dataset.id,
                RegisterNumber: dataset.reg,
                ShopName: dataset.name
            }
        });
        this.GetDetail();
    },
    //时间选择picker
    bindDateChange: function (e) {
        var myDate = e.detail.value;
        var dateType = parseInt(this.data.dateType);
        var date = myDate, datestr = myDate;
        switch (dateType) {
            case 1://天
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                datestr = date;
                break;
            case 2://周
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                //获取第几周
                var weeknum = app.common.getMonthWeek(date);
                datestr = date + "(第" + weeknum + "周)";
                break;
            case 3://月
                date = app.common.dateFormat(myDate, 'yyyy-MM');
                datestr = date;
                date=date+'-01';
                break;
        }
        this.setData({
            mydate: {
                nowdate: date,
                datestr: datestr
            }
        });
        this.GetDetail();
    },

    // 点击选择日期
    // bindChange: function (e) {
    //     const val = e.detail.value
    //     this.setData({
    //         year: this.data.years[val[0]],
    //         month: this.data.months[val[1]],
    //         day: this.data.days[val[2]]
    //     })
    // },
    //日期类型改变
    ChangeDateType: function (e) {
        var myDate=this.data.mydate.nowdate;
        var dataset = e.currentTarget.dataset;
        var dateType = parseInt(dataset.tp);
        var date = myDate, datestr = myDate;
        switch (dateType) {
            case 1://天
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                datestr = date;
                break;
            case 2://周
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                //获取第几周
                var weeknum = app.common.getMonthWeek(date);
                datestr = date + "(第" + weeknum + "周)";
                break;
            case 3://月
                date = app.common.dateFormat(myDate, 'yyyy-MM');
                datestr = date;
                date=date+'-01';
                break;
        }
        this.setData({
            mydate: {
                nowdate: date,
                datestr: datestr
            },
            dateType:dateType
        });
        this.GetDetail();
    },
    //设置报表图数据
    SetReportChart: function () {
        var _this = this;
        var colorI = 0;
        var colormax = _this.data.colorarry.length;
        var bossach = _this.data.bossach;
        if (bossach == null)
            return;
        //业绩报表图
        var yjChar = bossach.BTIChar;
        var yjseries = [];
        if (yjChar != null && yjChar.length > 1) {
            for (var i = 0; i < yjChar.length; i++) {
                var obj = {
                    "name": yjChar[i].ItemName,
                    "data": yjChar[i].ItemValue,
                    "color": _this.data.colorarry[colorI]
                }
                yjseries.push(obj);
                colorI = (colorI + 1) >= colormax ? 0 : (colorI + 1);
            }
            //初始化业绩报表图
            new wxCharts({
                canvasId: 'pieCanvas',
                type: 'pie',
                series: yjseries,
                width: 300,
                height: 300,
                dataLabel: true
            });
        }

        //员工业绩图
        var ygChar = bossach.YJ;
        var ygname = [];
        var ygdata = [];
        if (ygChar != null && ygChar.length > 1) {
            for (var i = 0; i < ygChar.length; i++) {
                ygname.push(ygChar[i].ItemName);
                ygdata.push(ygChar[i].ItemValue);
            }
            //初始化员工业绩报表图
            new wxCharts({
                canvasId: 'columnCanvas',
                type: 'column',
                categories: ygname,
                series: [{
                    "name": "员工业绩图",
                    "data": ygdata
                }],
                yAxis: {
                    format: function (val) {
                        return val;
                    }
                },
                width: 300,
                height: 300,
                dataLabel: true
            });
        }
        //项目业绩图
        var xmChar = bossach.XM;
        var xmseries = [];
        if (xmChar != null && xmChar.length > 1) {
            for (var i = 0; i < xmChar.length; i++) {
                var obj = {
                    name: xmChar[i].ItemName,
                    data: xmChar[i].ItemValue,
                    color: _this.data.colorarry[colorI]
                }
                xmseries.push(obj);
                colorI = (colorI + 1) >= colormax ? 0 : (colorI + 1);
            }
            //初始化项目报表图
            new wxCharts({
                canvasId: 'projectCanvas',
                type: 'pie',
                series: xmseries,
                width: 300,
                height: 300,
                dataLabel: true
            });
        }
        //客流量图
        var ksChar = bossach.KS;
        var ksname = [];
        var ksdata = [];
        var kscddata = [];
        if (ygChar != null && ygChar.length > 1) {
            for (var i = 0; i < ksChar.length; i++) {
                ksname.push(ksChar[i].Date);
                ksdata.push(ksChar[i].BillCount);
                kscddata.push(ksChar[i].CardBillCount);
            }
            //初始化员工业绩报表图
            new wxCharts({
                canvasId: 'lineCanvas',
                type: 'line',
                categories: ksname,
                series: [{
                    "name": "总客数",
                    "data": ksdata
                }, {
                    "name": "会员客数",
                    "data": kscddata
                }],
                yAxis: {
                    format: function (val) {
                        return val;
                    },
                    min: 0
                },
                width: 360,
                height: 240,
                dataLabel: false
            });
        }
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        wx.setNavigationBarTitle({
          title: '业绩查询'
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        // // 业绩报表图
        // new wxCharts({
        //     canvasId: 'pieCanvas',
        //     type: 'pie',
        //     series: [{
        //         name: '服务业绩',
        //         data: 30,
        //         color: '#8fc31f'
        //     }, {

        //         name: '产品业绩',
        //         data: 30,
        //         color: '#f35833'

        //     }, {

        //         name: '充卡业绩',
        //         data: 30,
        //         color: '#00ccff'

        //     }],

        //     width: 375,
        //     height: 250,
        //     dataLabel: true
        // });
        // // 员工业
        // new wxCharts({
        //     canvasId: 'columnCanvas',
        //     type: 'column',
        //     categories: ['刘德建', '钟流强', '王李强', '王枭', 'aleige',],
        //     series: [{
        //         name: '员工业绩图',
        //         data: [15, 20, 45, 37, 4]
        //     }],
        //     yAxis: {
        //         format: function (val) {
        //             return val;
        //         }
        //     },
        //     width: 375,
        //     height: 300,
        //     dataLabel: true
        // });
        // //项目业绩图
        // new wxCharts({
        //     canvasId: 'projectCanvas',
        //     type: 'pie',
        //     series: [{
        //         name: '烫发',
        //         data: 30,
        //         color: '#8fc31f'
        //     }, {

        //         name: '洗吹',
        //         data: 30,
        //         color: '#373742'

        //     }, {

        //         name: '创意洗剪吹',
        //         data: 30,
        //         color: '#f35833'

        //     },
        //     {

        //         name: '总监洗剪吹',
        //         data: 30,
        //         color: '#00ccff'

        //     },
        //     {

        //         name: '其他项目',
        //         data: 30,
        //         color: '#ffcc00'

        //     }
        //     ],

        //     width: 375,
        //     height: 250,
        //     dataLabel: true
        // });
        // //客流量图
        // new wxCharts({
        //     canvasId: 'lineCanvas',
        //     type: 'line',
        //     categories: ['2018-02-26', '2018-02-26', '2018-02-26', '2018-02-26', '2018-02-26'],
        //     series: [{
        //         name: '总客数',
        //         data: [5, 6, 7, 8, 9]
        //     }, {
        //         name: '会员客数',
        //         data: [1, 2, 3, 4, 5]
        //     }],
        //     yAxis: {
        //         format: function (val) {
        //             return val;
        //         },
        //         min: 0
        //     },
        //     width: 360,
        //     height: 240,
        //     dataLabel: false
        // });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})